<template>
  <el-container>
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        class="aaa"
        mode="horizontal"
        background-color="#23262e"
        active-text-color="#fff"
        text-color="#fff"
        default-active="3"
        unique-opened
      >
        <el-submenu index="3">
          <template slot="title">
            <img class="img" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="img" src="../assets/logo.png" alt="" v-else />
            个人中心
          </template>
          <el-menu-item index="3-1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="3-2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="3-3"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="1" @click="send"><i class="el-icon-edit"></i>退出</el-menu-item>
      </el-menu>
    </el-header>

    <el-container>
      <el-aside width="200px">
        <el-menu
          router
          mode="vertical"
          background-color="#23262e"
          text-color="#fff"
          default-active="1"
          unique-opened
        >
          <div class="box">
            <img class="img" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="img" src="../assets/logo.png" alt="" v-else />
            <p>
              欢迎<span>{{ user.nickname || user.username }}</span>
            </p>
          </div>

          <!-- 循环菜单 -->
          <template v-for="item in menu">
            <!-- el-menu-item 菜单的每一次项 -->
            <el-menu-item
              :index="item.indexPath"
              :key="item.indexPath"
              v-if="item.children === null"
              ><i :class="item.icon"> </i>{{ item.title }}</el-menu-item
            >

            <!-- el-submenu 菜单里面的二级菜单 -->
            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <!-- template 子菜单里面的标题 -->
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item
                :index="item2.indexPath"
                v-for="item2 in item.children"
                :key="item2.indexPath"
                ><i :class="item2.icon"></i>{{ item2.title }}</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>

      <!--  内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMeunAPI } from '@/api/menu'
import { mapState } from 'vuex'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },

  methods: {
    send() {
      this.$confirm('确定退出吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('user/updatedToken', '')
          this.$store.commit('user/updatedget', {})
          this.$router.push('/login')
        })
        .catch(() => {})
    },

    async getMeun() {
      const { data: res } = await getMeunAPI()
      // console.log(res)
      this.menu = res.data
    }
  },

  computed: {
    ...mapState('user', ['user'])
  },

  // 生命周期函数
  created() {
    this.$store.dispatch('user/getUser')
    this.getMeun()
  }
}
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
}
// 头部
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #23262e;
  height: 60px;
}
// 侧边栏
.el-aside {
  background-color: #23262e;
  height: 100%;
}
// 内容
.el-main {
  background-color: #f5f5f5;
}
.el-menu {
  height: 100%;
  width: 100%;
  overflow: hidden;
}
.img {
  height: 35px;
  border-radius: 50%;
  background-color: #fff;
  margin-right: 10px;
}
.aaa {
  margin-left: 70%;
}
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70px;
  p {
    color: #fff;
    font-size: 13px;
  }
}
</style>
